/**
 * 视频时间格式转秒数
 * @param time
 */
export function convertToSeconds(time: any) {
    const parts = time.split(':');
    let seconds = 0;
    if (parts.length === 3) {
        seconds += parseInt(parts[0]) * 3600;
        seconds += parseInt(parts[1]) * 60;
    } else if (parts.length === 2) {
        seconds += parseInt(parts[0]) * 60;
    }
    seconds += parseInt(parts[parts.length - 1]);
    return seconds;
}

/**
 * @description: 剩余时间
 */
export function getTimeArr(now = new Date()) {
    // const Y = now.getFullYear()
    // const M = now.getMonth() + 1
    // const D = now.getDate()
    const h = now.getHours();
    const m = now.getMinutes();
    const s = now.getSeconds();
    return [
        // ...toArr(Y),
        // ...toArr(M),
        // ...toArr(D),
        ...toArr(h),
        ...toArr(m),
        ...toArr(s),
    ];
}

// 更换数组类型
function toArr(n: any) {
    return n >= 10 ? ("" + n).split("").map((item) => Number(item)) : [0, n];
}

export enum FormatsEnums {
    YMD = "Y-m-d",
    YMDHIS = "Y-m-d H:i:s",
    _YMD = "Y年m月d日",
    _YMDHIS = "Y年m月d日 H时i分",
    TIME = "H:i:s"
}

export const dateFormat = function (timestamp: number, formats: FormatsEnums): string {
    // formats格式包括
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y年m月d日
    // 4. Y年m月d日 H时i分
    formats = formats || "Y-m-d";

    const zero = function (value: number): string | number {
        if (value < 10) {
            return "0" + value;
        }
        return value;
    };

    const myDate = timestamp ? new Date(timestamp) : new Date();

    const year = myDate.getFullYear();
    const month = zero(myDate.getMonth() + 1);
    const day = zero(myDate.getDate());

    const hour = zero(myDate.getHours());
    const minite = zero(myDate.getMinutes());
    const second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/gi, function (matches: string): any {
        return {
            Y: year,
            m: month,
            d: day,
            H: hour,
            i: minite,
            s: second
        }[matches];
    });
};

